package com.shinhye.mobilelottery.ui;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.shinhye.mobilelottery.ConstantValue;
import com.shinhye.mobilelottery.R;
import com.shinhye.mobilelottery.engine.CommonInfoEngine;
import com.shinhye.mobilelottery.net.protocol.Element;
import com.shinhye.mobilelottery.net.protocol.Message;
import com.shinhye.mobilelottery.net.protocol.Oelement;
import com.shinhye.mobilelottery.net.protocol.element.IssueInfoElement;
import com.shinhye.mobilelottery.utils.BeanFactory;
import com.shinhye.mobilelottery.utils.PromptManager;
import com.shinhye.mobilelottery.utils.TimeUtils;

public class HallUI2 extends BaseUI{
	private int[] logoResIds;
	private int[] titleResIds;
	private List<TextView> needUpdate;
	private ListView lv_content;
	
	public HallUI2(Context context) {
		super(context);
	}
	
	@Override
	protected void onCreate() {
		View showView = View.inflate(getContext(), R.layout.il_hall2, null);
		lv_content = (ListView) showView.findViewById(R.id.ii_hall_lottery_list);
		
		//onCreate方法在父类构造方法里，所以最先执行。其次是成员变量的初始化，最后才是子类构造方法的执行。
		//所以此处只能在此初始化成员变量
		logoResIds = new int[] {R.drawable.id_ssq, R.drawable.id_3d, R.drawable.id_qlc};
		titleResIds = new int[] {R.string.is_hall_ssq_title, R.string.is_hall_3d_title, R.string.is_hall_qlc_title};
//		logoResIds = new int[] {R.drawable.id_ssq, R.drawable.id_3d, R.drawable.id_qlc, R.drawable.id_ssq, R.drawable.id_3d, R.drawable.id_qlc, R.drawable.id_ssq, R.drawable.id_3d, R.drawable.id_qlc};
//		titleResIds = new int[] {R.string.is_hall_ssq_title, R.string.is_hall_3d_title, R.string.is_hall_qlc_title, R.string.is_hall_ssq_title, R.string.is_hall_3d_title, R.string.is_hall_qlc_title, R.string.is_hall_ssq_title, R.string.is_hall_3d_title, R.string.is_hall_qlc_title};
		needUpdate = new ArrayList<TextView>();
		
		MyAdapter adapter = new MyAdapter();
		lv_content.setAdapter(adapter);
		
		setContentView(showView);
	}
	
	class MyAdapter extends BaseAdapter {
		@Override
		public int getCount() {
			return logoResIds.length;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view;
			ViewHolder holder;
			if(convertView != null) {
				view = convertView;
				holder = (ViewHolder) view.getTag();
			} else {
				view = View.inflate(getContext(), R.layout.il_hall_lottery_item, null);
				holder = new ViewHolder();
				holder.logo = (ImageView) view.findViewById(R.id.ii_hall_lottery_logo);
				holder.title = (TextView) view.findViewById(R.id.ii_hall_lottery_title);
				holder.summary = (TextView) view.findViewById(R.id.ii_hall_lottery_summary);
				holder.bet = (ImageView) view.findViewById(R.id.ii_hall_lottery_bet);
				view.setTag(holder);
				needUpdate.add(holder.summary);
			}
			
			holder.logo.setImageResource(logoResIds[position]);
			holder.title.setText(titleResIds[position]);
			holder.bet.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
				}
			});
			holder.summary.setTag(position);
			
			return view;
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}
	}
	
	static class ViewHolder {
		ImageView logo;
		TextView title;
		TextView summary;
		ImageView bet;
	}
	
	private void getIssueInfo() {
		new MyAsyncTask<Integer>() {
			@Override
			protected Message doInBackground(Integer... params) {
				int id = params[0];
				CommonInfoEngine infoEngine = BeanFactory.getImpl(CommonInfoEngine.class);
				Message res = infoEngine.getIssuesInfo(id);
				return res;
			}

			@Override
			protected void onPostExecute(Message result) {
				if(result == null)
					return;
				
				Oelement oelement = result.getBody().getOelement();
				if(ConstantValue.SUCCESS.equals(oelement.getErrorcode())) {
					changeNotice(result.getBody().getElements().get(0));
				} else {
					PromptManager.showErrorDialog(getContext(), oelement.getErrormsg());
				}
			}
		}.execute(ConstantValue.SSQ);
	}
	
	private void changeNotice(Element element) {
		IssueInfoElement infoEle = (IssueInfoElement) element;
		String issue = infoEle.getIssue().getValue();
		String lasttime = TimeUtils.getStandardTime(infoEle.getLasttime().getValue());
		String text = getContext().getResources().getString(R.string.is_hall_common_summary);
		text = StringUtils.replaceEach(text, new String[] { "ISSUE", "TIME" }, new String[] { issue, lasttime });
		
//		TextView textView = needUpdate.get(0);
//		if (textView != null) {
//			textView.setText(text);
//		}
		
		//TODO -0- 由于tag会通过convertView转化而重复使用，如果ListView条目过多会导致多个条目出现一样的值
		TextView textView = (TextView) lv_content.findViewWithTag(0);
		if (textView != null) {
			textView.setText(text);
		}

	}
	
	@Override
	public int getId() {
		return ConstantValue.VIEW_HALL;
	}

	@Override
	protected void setListener() {
		
	}

	@Override
	public void onResume() {
		getIssueInfo();
	}

}
